#include "DIALOG.h"
#include "MC_include.h"
#include "Maintask.h"

#define ID_FRAMEWIN_4     (GUI_ID_USER + 0x5a0)

#define ID_SEDIT_0  (GUI_ID_USER + 0x501)
#define ID_SEDIT_1  (GUI_ID_USER + 0x502)
#define ID_SEDIT_2  (GUI_ID_USER + 0x503)
#define ID_SBUTTON_0  (GUI_ID_USER + 0x504)
#define ID_SBUTTON_1  (GUI_ID_USER + 0x505)
#define ID_STEXT_0  (GUI_ID_USER + 0x506)
#define ID_STEXT_1  (GUI_ID_USER + 0x507)
#define ID_STEXT_2  (GUI_ID_USER + 0x508)
#define ID_SBUTTON_2  (GUI_ID_USER + 0x509)
#define ID_SBUTTON_3  (GUI_ID_USER + 0x50A)
#define ID_SBUTTON_4  (GUI_ID_USER + 0x50B)
#define ID_SBUTTON_5  (GUI_ID_USER + 0x50C)
#define ID_SBUTTON_6  (GUI_ID_USER + 0x50D)
#define ID_SBUTTON_7  (GUI_ID_USER + 0x50E)
#define ID_SBUTTON_8  (GUI_ID_USER + 0x50F)
#define ID_SBUTTON_9  (GUI_ID_USER + 0x510)
#define ID_SBUTTON_10  (GUI_ID_USER + 0x511)
#define ID_SBUTTON_11  (GUI_ID_USER + 0x512)
#define ID_SBUTTON_12  (GUI_ID_USER + 0x513)
#define ID_SBUTTON_13  (GUI_ID_USER + 0x514)
#define ID_STEXT_3  (GUI_ID_USER + 0x515)
#define ID_SBUTTON_14  (GUI_ID_USER + 0x516)
#define ID_SBUTTON_15  (GUI_ID_USER + 0x517)
#define ID_SBUTTON_16  (GUI_ID_USER + 0x518)

#define ID_SBUTTON_17  (GUI_ID_USER + 0x519)
#define ID_SEDIT_3  (GUI_ID_USER + 0x520)
extern uint8_t setWin_flag;
uint8_t S_Pressed = 0;

static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] =
{
    { FRAMEWIN_CreateIndirect, "Framewin", ID_FRAMEWIN_4, 0, 0, 800, 480, 0, 0x64, 0 },
    { EDIT_CreateIndirect, "Edit", ID_SEDIT_0, 90, 50, 90, 30, 0, 0x64, 0 },
    { EDIT_CreateIndirect, "Edit", ID_SEDIT_1, 90, 90, 90, 30, 0, 0x64, 0 },
    { EDIT_CreateIndirect, "Edit", ID_SEDIT_2, 90, 130, 90, 30, 0, 0x64, 0 },
    { BUTTON_CreateIndirect, "确认", ID_SBUTTON_0, 508, 391, 115, 40, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "取消", ID_SBUTTON_1, 639, 391, 115, 40, 0, 0x0, 0 },
    { TEXT_CreateIndirect, "X行程", ID_STEXT_0, 7, 50, 80, 30, 0, 0x0, 0 },
    { TEXT_CreateIndirect, "Y行程", ID_STEXT_1, 7, 90, 80, 30, 0, 0x0, 0 },
    { TEXT_CreateIndirect, "Z行程", ID_STEXT_2, 7, 130, 80, 30, 0, 0x64, 0 },
    { BUTTON_CreateIndirect, "1", ID_SBUTTON_2, 510, 47, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "2", ID_SBUTTON_3, 600, 47, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "3", ID_SBUTTON_4, 690, 47, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "4", ID_SBUTTON_5, 510, 130, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "5", ID_SBUTTON_6, 600, 130, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "6", ID_SBUTTON_7, 690, 130, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "7", ID_SBUTTON_8, 510, 215, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "8", ID_SBUTTON_9, 600, 215, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "9", ID_SBUTTON_10, 690, 215, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "-", ID_SBUTTON_11, 510, 300, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "0", ID_SBUTTON_12, 600, 300, 56, 56, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "BAK", ID_SBUTTON_13, 690, 300, 56, 56, 0, 0x0, 0 },
    { TEXT_CreateIndirect, "行程设置", ID_STEXT_3, 17, 15, 153, 26, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "清空", ID_SBUTTON_14, 200, 50, 80, 30, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "清空", ID_SBUTTON_15, 200, 90, 80, 30, 0, 0x0, 0 },
    { BUTTON_CreateIndirect, "清空", ID_SBUTTON_16, 200, 130, 80, 30, 0, 0x0, 0 },
	{ BUTTON_CreateIndirect, "清空", ID_SBUTTON_17, 200, 170, 80, 30, 0, 0x0, 0 },
	 { EDIT_CreateIndirect, "Edit", ID_SEDIT_3, 90, 170, 90, 30, 0, 0x64, 0 },
};

static void _cbDialog(WM_MESSAGE * pMsg)
{
    WM_HWIN hItem;
    int     NCode;
    int     Id;
    S_Pressed = 0;
    uint8_t num[2] = {12, 23};
    uint8_t readnum[2] = {0};
	char textbuff[10];
	int old_X,old_Y,old_Z;
	
    switch (pMsg->MsgId)
    {
        case WM_INIT_DIALOG:
            //
            // Initialization of 'Framewin'
            //
            hItem = pMsg->hWin;
            FRAMEWIN_SetSkinClassic(hItem);
            FRAMEWIN_SetActive(hItem, 1);
            FRAMEWIN_SetTitleHeight(hItem, 32);
            FRAMEWIN_SetFont(hItem, &GUI_Fontfont20B);
            FRAMEWIN_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            FRAMEWIN_SetText(hItem, "设置");
            FRAMEWIN_AddCloseButton(hItem, FRAMEWIN_BUTTON_RIGHT, 0);
            FRAMEWIN_SetClientColor(hItem, GUI_MAKE_COLOR(0x00E7E7E7));


            hItem = WM_GetDialogItem(pMsg->hWin, ID_SEDIT_0);
			sprintf(textbuff,"%d",MC_Config_param.X_Max_range);
            EDIT_SetText(hItem, textbuff);
            EDIT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            EDIT_SetFont(hItem, GUI_FONT_20B_1);

            hItem = WM_GetDialogItem(pMsg->hWin, ID_SEDIT_3);
            EDIT_SetText(hItem, "32");
            EDIT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            EDIT_SetFont(hItem, GUI_FONT_20B_1);
		
            //
            // Initialization of 'Edit'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_SEDIT_1);
			sprintf(textbuff,"%d",MC_Config_param.Y_Max_range);
            EDIT_SetText(hItem, textbuff);
            EDIT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            EDIT_SetFont(hItem, GUI_FONT_20B_1);
            //
            // Initialization of 'Edit'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_SEDIT_2);
			sprintf(textbuff,"%d",-MC_Config_param.Z_Max_range);
            EDIT_SetText(hItem, textbuff);
            EDIT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            EDIT_SetFont(hItem, GUI_FONT_20B_1);

            //
            // Initialization of 'Text'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_STEXT_0);
            TEXT_SetTextAlign(hItem, GUI_TA_RIGHT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, &GUI_Fontfont20B);
            //
            // Initialization of 'Text'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_STEXT_1);
            TEXT_SetTextAlign(hItem, GUI_TA_RIGHT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, &GUI_Fontfont20B);
            //
            // Initialization of 'Text'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_STEXT_2);
            TEXT_SetTextAlign(hItem, GUI_TA_RIGHT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, &GUI_Fontfont20B);
            //
            // Initialization of 'Text'
            //
            hItem = WM_GetDialogItem(pMsg->hWin, ID_STEXT_3);
            TEXT_SetTextAlign(hItem, GUI_TA_LEFT | GUI_TA_VCENTER);
            TEXT_SetFont(hItem, &GUI_Fontfont20B);
			
			BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_SBUTTON_0),&GUI_Font20);
			BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_SBUTTON_1),&GUI_Font20);
			BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_SBUTTON_14),&GUI_Font20);
			BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_SBUTTON_15),&GUI_Font20);
			BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_SBUTTON_16),&GUI_Font20);
			BUTTON_SetFont(WM_GetDialogItem(pMsg->hWin, ID_SBUTTON_17),&GUI_Font20);
            //按键初始化
            for(int i = ID_SBUTTON_2; i <= ID_SBUTTON_13; i ++)
            {
                hItem = WM_GetDialogItem(pMsg->hWin, i);
                BUTTON_SetFocussable(hItem, 0);  //按键失去聚焦
                BUTTON_SetFont(hItem, GUI_FONT_16B_1);
            }
			
			old_X =MC_Config_param.X_Max_range ;
			old_Y =MC_Config_param.Y_Max_range ;
			old_Z =MC_Config_param.Z_Max_range ;
			
            break;

        case WM_DELETE:
            setWin_flag = 0;
            break;

        case WM_NOTIFY_PARENT:
            Id    = WM_GetId(pMsg->hWinSrc);
            NCode = pMsg->Data.v;

            if(Id >= ID_SBUTTON_2  && Id <= ID_SBUTTON_13)
            {
                switch(NCode)
                {
                    case WM_NOTIFICATION_CLICKED:
                        S_Pressed = 1;

                    case WM_NOTIFICATION_RELEASED:
                    {
                        int Key;
						if(Id < ID_SBUTTON_13)
						{
							char acBuffer[20];
							BUTTON_GetText(pMsg->hWinSrc, acBuffer, sizeof(acBuffer)); /* Get the text of the button */
							Key = acBuffer[0];						
						}
						else
						{
							Key = GUI_KEY_BACKSPACE;
						}
                        GUI_SendKeyMsg(Key, S_Pressed); /*Send a key message to the focussed window */
                    }
                    break;

                    default:
                        WM_DefaultProc(pMsg);
                }
            }

            switch(Id)
            {
                case ID_SBUTTON_0:
                    switch(NCode)
                    {
						float val;
                        case WM_NOTIFICATION_CLICKED:
                            break;

                        case WM_NOTIFICATION_RELEASED:
							hItem = WM_GetDialogItem(pMsg->hWin, ID_SEDIT_0);
							EDIT_GetText(hItem, textbuff, 20);
							stringTofloat(textbuff,20,&val);
							printf("text1 %.1f\r\n",val);
							num[1] = (int)val >> 8;
							num[0] = (int)val & 0x00ff;
							printf("%d\r\n",(num[1]<<8) | num[0]);
                            I2C_EE_PageWrite(num, 0x10, 2);
							hItem = WM_GetDialogItem(pMsg->hWin, ID_SEDIT_1);
							EDIT_GetText(hItem, textbuff, 20);
							stringTofloat(textbuff,20,&val);
							num[1] = (int)val >> 8;
							num[0] = (int)val & 0x00ff;
							printf("%d\r\n",(num[1]<<8) | num[0]);
                            I2C_EE_PageWrite(num, 0x20, 2);
							hItem = WM_GetDialogItem(pMsg->hWin, ID_SEDIT_2);
							EDIT_GetText(hItem, textbuff, 20);
							stringTofloat(textbuff,20,&val);
							num[1] = (int)val >> 8;
							num[0] = (int)val & 0x00ff;
							printf("%d\r\n",(num[1]<<8) | num[0]);
                            I2C_EE_PageWrite(num, 0x30, 2);
                            break;
                    }

                    break;

                case ID_SBUTTON_1:
                    switch(NCode)
                    {
                        case WM_NOTIFICATION_RELEASED:
                            //I2C_EE_BufferRead(readnum, 0x10, 2);
                            //printf("%d,%d\r\n", readnum[0], readnum[1]);
							MC_Config_param.X_Max_range = old_X;
							MC_Config_param.X_Max_range = old_Y;
							MC_Config_param.X_Max_range = old_Z;
                            break;
                    }

                    break;

            }

            break;

        default:
            WM_DefaultProc(pMsg);
            break;

    }
}

WM_HWIN CreateSetwin(void);
WM_HWIN CreateSetwin(void)
{
    WM_HWIN hWin;

    hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0);

    while(setWin_flag)
    {
        GUI_Delay(30);
    }

    return hWin;
}

// USER START (Optionally insert additional public code)
// USER END

/*************************** End of file ****************************/
